<script setup>
//import {excelApi} from "../../../api/axios";
//import {MINIO_HOST} from "../../../const";
import {onMounted, ref, shallowReactive, shallowRef} from "vue";
import router from "../../../../router";
import {dateFormat, myRemove, myPage, myRemoveBatch} from "../../../../util";
import {pageApi, removeApi, removeBatchApi} from "../../../../api/menu.js";
import MyNav from "../../../../components/MyNav.vue";
import MyHead from "../../../../components/MyHead.vue";
import MyTable from "../../../../components/MyTable.vue";
import MyPager from "../../../../components/MyPager.vue";
import MyList from "../../../../components/MyList.vue";

// 当前父菜单ID和菜单名称
let pid = sessionStorage.getItem('pid');
let pname = sessionStorage.getItem('pname');

/* ==================== 分页查询 ==================== */
// 表格数据 + 分页数据
let records = ref();
let pageInfo = shallowReactive({'pageNum': 1, 'pageSize': 5, 'fn': page});
// 分页查询条件字段：菜单名称
let menuName = shallowRef();

// 分页函数
async function page(pageNum = pageInfo['pageNum'], pageSize = pageInfo['pageSize']) {

  // 分页基础配置
  let config = {
    'api': pageApi,
    'params': {pageNum, pageSize, pid},
    'records': records,
    'pageInfo': pageInfo
  }

  // 附加为分页条件
  if (menuName.value) config['params']['name'] = menuName.value;

  // 发送分页请求
  await myPage(config);

  // 将pname融入表格数据的每一项
  Object.values(records.value).forEach(menu => menu['name'] = pname + ' / ' + menu['name']);
}

/* ==================== 查看详情 ==================== */
// 菜单详情抽屉 + 菜单详情列表数据
let detailDrawer = shallowRef();
let detailItems = ref();

function detail(record) {
  detailItems.value = [
    {'label': '菜单名称', 'value': record['name']},
    {'label': '菜单图标', 'value': record['icon'], 'type': 'icon'},
    {'label': '菜单序号', 'value': record['sort'], 'type': 'tag'},
    {'label': '菜单地址', 'value': record['url']},
    {'label': '创建时间', 'value': record['created'], 'format': dateFormat},
    {'label': '修改时间', 'value': record['updated'], 'format': dateFormat},
    {'label': '菜单描述', 'value': record['info'], 'type': 'card', 'tooltip': false},
  ];
  detailDrawer.value = true;
}

/* ==================== 单条添加 ==================== */
function insert() {
  router.push('/SubMenuInsert');
}

/* ==================== 单条修改 ==================== */
function update(row) {
  sessionStorage.setItem('menu', JSON.stringify(row));
  router.push('/SubMenuUpdate');
}

/* ==================== 单条删除 ==================== */
function remove(row) {
  myRemove({'id': row['id'], 'api': removeApi, 'fn': page});
}

/* ==================== 批量删除 ==================== */
// 批量删除主键数组
let ids = [];

function selectionChange(rows) {
  ids = rows.map(e => e['id']);
}

function removeBatch() {
  myRemoveBatch({'ids': ids, 'api': removeBatchApi, 'fn': page})
}

/* ==================== 按菜单名称模糊查询 ==================== */
function pageLikeName(val) {

  // 仅当输入框有值，或者当前处于按条件分页状态时，发送分页请求
  if (val || menuName.value) {
    menuName.value = val;
    page();
  }
}

/* ==================== 打印报表 ==================== */
function excel() {
  // excelApi('/menu/excel', '菜单统计表');
}

/* ==================== 加载函数 ==================== */
onMounted(() => page());

</script>

<template>

  <my-nav :items="[
    {'icon': 'Avatar', 'label': '用户管理'},
    {'icon': 'User', 'label': '菜单列表（一级菜单）', 'url': '/Menu'},
    {'icon': 'User', 'label': '菜单列表（二级菜单）'},
  ]"/>

  <my-head :items="[
    {'type': 'ipt', 'span': 5, 'placeholder': '按菜单名称模糊搜索', 'fn': pageLikeName},
    {'type': 'insertBtn', 'offset': 16, 'fn': insert},
    {'type': 'deleteBtn', 'fn': removeBatch},
    {'type': 'excelBtn', 'fn': excel},
  ]"/>

  <my-table v-if="records" :records="records" :columns="[
    {'column': ['菜单信息', '菜单名称'], 'property': 'name', 'width': 240},
    {'column': ['菜单信息', '菜单图标'], 'property': 'icon', 'type': 'icon', 'width': 80, 'sortable': false},
    {'column': ['菜单信息', '菜单序号'], 'property': 'sort', 'type': 'tag'},
    {'column': ['菜单信息', '菜单地址'], 'property': 'url'},
    {'column': ['菜单信息', '菜单描述'], 'property': 'info', 'type': 'card'},
  ]" :buttons="[
    {'type': 'info', 'fn': detail},
    {'type': 'update', 'fn': update},
    {'type': 'delete', 'fn': remove},
  ]" :selection-change="selectionChange"/>

  <my-pager v-if="records" :page-info="pageInfo"/>

  <el-drawer v-model="detailDrawer" size="30%">
    <my-list title="记录详情" :items="detailItems"/>
  </el-drawer>

</template>

<style scoped lang="scss">

</style>